package com.sms.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.system.comm.util.Snowflake;
import com.system.common.mapper.BisBusinessOrderMapper;
import com.system.common.mapper.BisCustomUserMapper;
import com.system.common.mapper.BisNurseProjectMapper;
import com.system.common.mapper.BisNurseTypeMapper;
import com.system.common.mapper.BisOrderDepositMapper;
import com.system.common.pojo.BisBusinessOrder;
import com.system.common.pojo.BisBusinessOrderExample;
import com.system.common.pojo.BisCustomUser;
import com.system.common.pojo.BisNurseType;
import com.system.common.pojo.BisOrderDeposit;


@Service
public class BisBusinessServiceImpl implements BisBusinessService{
	@Autowired
	private BisBusinessOrderMapper bisBusinessOrdermapper;
	@Autowired
	private BisNurseProjectMapper bisNurseProjectMapper;
	@Autowired
	private BisNurseTypeMapper bisNurseTypeMapper;
	@Autowired
	private BisCustomUserMapper bisCustomUserMapper;
	@Autowired
	private BisOrderDepositMapper bisOrderDepositMapper;
	//查询所有预约的
	@SuppressWarnings("rawtypes")
	@Override
	public List<Map<String, Object>> queryAll(Map map) {
		return this.bisBusinessOrdermapper.selectAll(map);
	}
	@Override
	public List<Map<String, Object>> selectAll(Map<String,Object> map) {	
		List<Map<String, Object>> projects=new ArrayList<>();
		for (Map<String, Object> pro : bisNurseProjectMapper.pListAll(map)) {
			String isDel=(String) pro.get("is_del");
			if (isDel.equals("0")) {
				projects.add(pro);
			}
		}
		return projects;
	}
	@Override
	public List<BisNurseType> typeSelect() {
		return bisNurseTypeMapper.selectByExample(null);
	}
	@Override
	public void update(BisBusinessOrder bisBusinessOrder) {
		bisBusinessOrdermapper.updateByPrimaryKey(bisBusinessOrder);
	}
	@Override
	public BisBusinessOrder selectId(String orderId) {
		return bisBusinessOrdermapper.selectByPrimaryKey(orderId);
	}
	@Override
	public void addBisBusinessOrder(String account,String projectId,String price,String time,String note,String content) {
		   Snowflake snowflake=new Snowflake(0, 0);
		   Long lid=snowflake.nextId();
		   String id=lid+"";
		   id=id.substring(0,8);
		   BisCustomUser bisCustomUser=new BisCustomUser();
		   bisCustomUser.setCustomUserId(id);
		   bisCustomUser.setMobile(account);
		   bisCustomUser.setUsername(account);
		   bisCustomUser.setSex("3");
		   bisCustomUser.setWechatStatus("1");
		   bisCustomUser.setBeautyNum(0);
		   bisCustomUser.setBagNum(0);
		   bisCustomUser.setIsDel("0");
		   bisCustomUser.setEnrollMode("2");
		   bisCustomUser.setEnrollTime(new Date());
		   this.bisCustomUserMapper.insert(bisCustomUser);
		   Snowflake snowflakes=new Snowflake(0, 0);
		   Long lids=snowflakes.nextId();
		   String ids=lids+"";
		   ids=ids.substring(0,11);
		   BisBusinessOrder bisBusinessOrder=new BisBusinessOrder();
		   bisBusinessOrder.setOrderId(ids);
		   bisBusinessOrder.setOrderContent(content);
		   bisBusinessOrder.setProjectId(projectId);
		   bisBusinessOrder.setShopId("10000001");
		  
		   bisBusinessOrder.setPayMoney(Double.parseDouble(price));
		   bisBusinessOrder.setCustomUserId(bisCustomUser.getCustomUserId());		
		   time=time.replaceAll("T", " ")+":00";
		   SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); 
		   Date date = null;
		    
			try {
				date = sdf.parse(time);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		   bisBusinessOrder.setSubscribeTime(date);
		   bisBusinessOrder.setOrderType("1");
		   bisBusinessOrder.setOrderMoney(Double.parseDouble(price));
		   bisBusinessOrder.setBuyNum(1);
		   bisBusinessOrder.setOrderStatus("1");
		   bisBusinessOrder.setOrderRemark(note);
		   bisBusinessOrder.setCreateTime(new Date());
		   bisBusinessOrder.setOrderSource("2");
		   bisBusinessOrder.setModifyStatus("0");
		   bisBusinessOrder.setFreeNum(0);
		   this.bisBusinessOrdermapper.insert(bisBusinessOrder);
		   Snowflake snowflakess=new Snowflake(0, 0);
		   Long lidss=snowflakess.nextId();
		   String idss=lidss+"";
		   idss=idss.substring(0,16);
		   BisOrderDeposit bisOrderDeposit=new BisOrderDeposit();
		   bisOrderDeposit.setDepositId(idss);
		   bisOrderDeposit.setOrderId(bisBusinessOrder.getOrderId());
		   bisOrderDeposit.setDepositMoney(0.00);
		   bisOrderDeposit.setCustomUserId(bisCustomUser.getCustomUserId());
		   this.bisOrderDepositMapper.insert(bisOrderDeposit);
		
	}
	@Override
	public BisCustomUser queryId(Map<String,Object> map) {
		String orderId=(String)map.get("orderId");
		BisBusinessOrderExample example=new BisBusinessOrderExample();
		com.system.common.pojo.BisBusinessOrderExample.Criteria cri=example.createCriteria();
		if (StringUtils.isNotBlank(orderId)) {
			cri.andOrderIdEqualTo(orderId);
		}
		BisBusinessOrder order=bisBusinessOrdermapper.selectByPrimaryKey(orderId);	
		return bisCustomUserMapper.selectByPrimaryKey(order.getCustomUserId());
	}
	@Override
	public boolean updateBisCustomUser(BisCustomUser bisCustomUser) {
		bisCustomUserMapper.updateByPrimaryKeySelective(bisCustomUser);
		return true;
	}
	
}
